package com.mxgraph.online;

import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Properties;

public class OssServlet extends HttpServlet {

    private static Properties porp;
    private static String SESSION_NAME;
    private static String METHOD_KEY;
    private static String AUTH_METHOD_CALLBACK;
    private static String OPEN_METHOD_CALLBACK;
    private static String AUTH_TOKEN_NAME;

    static{
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        InputStream in = cl.getResourceAsStream("/oss.properties");
        porp = new Properties();
        try {
            porp.load(in);
            SESSION_NAME = porp.getProperty("dmn.oss.sessionId");
            METHOD_KEY = porp.getProperty("dmn.oss.method_key");
            AUTH_METHOD_CALLBACK = porp.getProperty("dmn.oss.auth_method_callback");
            OPEN_METHOD_CALLBACK = porp.getProperty("dmn.oss.open_method_callback");
            AUTH_TOKEN_NAME = porp.getProperty("dmn.oss.auth_token_name");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        String methodKey = request.getParameter(METHOD_KEY);
        String initConfig = request.getParameter("initConfig");
        if("true".equals(initConfig)){
            JsonObject jo = new JsonObject();
            jo.addProperty("authUri",porp.getProperty("dmn.oss.authUri"));
            jo.addProperty("saveUri",porp.getProperty("dmn.oss.saveUri"));
            jo.addProperty("openUri",porp.getProperty("dmn.oss.openUri"));
            jo.addProperty("pickUri",porp.getProperty("dmn.oss.pickUri"));
            jo.addProperty("sessionId",SESSION_NAME);
            resp.getWriter().println(jo.toString());
        } else {
            if(AUTH_METHOD_CALLBACK.equals(methodKey)){
                authCallBack(request, resp);
            } else if(OPEN_METHOD_CALLBACK.equals(methodKey)) {
                openCallBack(request, resp);
            }
        }
    }


    private void authCallBack(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/" + "oss.html";
        handleCookie(request, resp, basePath);
    }

    private void openCallBack(HttpServletRequest request, HttpServletResponse resp) throws IOException {
        String path = request.getContextPath();
        String htId = request.getParameter("htId");
        String htBbId = request.getParameter("htBbId");
        String title = request.getParameter("title");
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
        if(!StringUtils.isEmpty(htId)){
            basePath += ("?htId=" + htId);
            if(!StringUtils.isEmpty(htBbId)){
                basePath += ("&htBbId=" + htBbId);
            }
            if(!StringUtils.isEmpty(title)){
                basePath += ("#O" + URLEncoder.encode(title,"UTF-8"));
            }
        }
        handleCookie(request, resp, basePath);
    }

    private void handleCookie(HttpServletRequest request, HttpServletResponse resp, String basePath) throws IOException {
        String sessionId = request.getParameter(AUTH_TOKEN_NAME);
        if(sessionId == null){
            resp.setHeader("Content-Type", "text/html; charset=UTF-8");
            resp.getWriter().println("会话信息不存在");
            return;
        }
        Cookie sessionCookie = new Cookie(SESSION_NAME, sessionId);
        sessionCookie.setPath("/");
        resp.addCookie(sessionCookie);
        resp.sendRedirect(basePath);
    }
}
